package com.smedi.ismedi.personnel.core.port.adapter.persistence.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smedi.ismedi.personnel.core.domain.aggregatemodel.Signature;
import com.smedi.ismedi.personnel.core.domain.repository.SignatureRepository;
import com.smedi.ismedi.personnel.core.port.adapter.persistence.model.SignatureEntity;
import com.smedi.ismedi.personnel.core.port.adapter.persistence.mybatis.mapper.SignatureMapper;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @Author: xulinjun
 * @Date: 2021-6-9 19:49
 */
@Slf4j
@Service
@AllArgsConstructor
public class SignatureRepositoryImpl extends ServiceImpl<SignatureMapper, SignatureEntity> implements SignatureRepository, IService<SignatureEntity> {
    private final DozerUtil dozerUtil;

    @Override
    public Signature selectSignatureBySignatureUUID(String signatureUUID) {
        QueryWrapper<SignatureEntity> wrapper = new QueryWrapper();
        wrapper.eq("uuid", signatureUUID);
        SignatureEntity signatureEntity = this.getBaseMapper().selectOne(wrapper);
        if (signatureEntity != null) {
            return dozerUtil.convertor(signatureEntity, Signature.class);
        } else {
            return null;
        }
    }

    @Override
    public Signature selectSignatureByPersonId(Long personId) {
        QueryWrapper<SignatureEntity> wrapper = new QueryWrapper();
        wrapper.eq("person_id", personId);
        SignatureEntity signatureEntity = this.getBaseMapper().selectOne(wrapper);
        if (signatureEntity != null) {
            return dozerUtil.convertor(signatureEntity, Signature.class);
        } else {
            return null;
        }
    }

    @Override
    public Signature doInsert(Signature data) {
        SignatureEntity signatureEntity = dozerUtil.convertor(data, SignatureEntity.class);
        this.getBaseMapper().insert(signatureEntity);
        return dozerUtil.convertor(signatureEntity, Signature.class);
    }

    @Override
    public Signature doUpdate(Signature data) {
        SignatureEntity signatureEntity = dozerUtil.convertor(data, SignatureEntity.class);
        this.getBaseMapper().updateById(signatureEntity);
        return dozerUtil.convertor(signatureEntity, Signature.class);
    }
}
